package io.xccit.race.controller;

import java.util.List;

import io.xccit.race.domain.RaceCompetitionRules;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.xccit.common.annotation.Log;
import io.xccit.common.core.controller.BaseController;
import io.xccit.common.core.domain.AjaxResult;
import io.xccit.common.enums.BusinessType;
import io.xccit.race.domain.RaceCompetitionDetail;
import io.xccit.race.service.IRaceCompetitionDetailService;
import io.xccit.race.service.IRaceCompetitionRulesService;

/**
 * 竞赛详情Controller
 * 
 * @author ywx
 * @date 2025-10-9
 */
@RestController
@RequestMapping("/race/competitionDetail")
public class RaceCompetitionDetailController extends BaseController
{
    @Autowired
    private IRaceCompetitionDetailService raceCompetitionDetailService;
    
    @Autowired
    private IRaceCompetitionRulesService raceCompetitionRulesService;

    /**
     * 查询竞赛详情列表
     */
    @PreAuthorize("@ss.hasPermi('race:competitionDetail:list')")
    @GetMapping("/list")
    public AjaxResult list(RaceCompetitionDetail raceCompetitionDetail)
    {
        List<RaceCompetitionDetail> list = raceCompetitionDetailService.selectRaceCompetitionDetailList(raceCompetitionDetail);
        return success(list);
    }

    /**
     * 获取竞赛详情详细信息
     */
    @PreAuthorize("@ss.hasPermi('race:competitionDetail:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(raceCompetitionDetailService.selectRaceCompetitionDetailById(id));
    }

    /**
     * 根据竞赛ID获取竞赛详情
     */
    @GetMapping("/competition/{competitionId}")
    public AjaxResult getInfoByCompetitionId(@PathVariable("competitionId") Long competitionId)
    {
        return success(raceCompetitionDetailService.selectRaceCompetitionDetailByCompetitionId(competitionId));
    }

    /**
     * 根据竞赛ID获取赛制信息
     */
    @GetMapping("/competition/{competitionId}/rules")
    public AjaxResult getCompetitionRules(@PathVariable("competitionId") Long competitionId)
    {
        try {
            // 获取竞赛详情
            RaceCompetitionDetail competitionDetail = raceCompetitionDetailService.selectRaceCompetitionDetailByCompetitionId(competitionId);
            
            if (competitionDetail == null) {
                return error("该竞赛不存在或未配置详情信息");
            }
            
            if (competitionDetail.getRulesId() == null) {
                return error("该竞赛未配置赛制信息");
            }
            
            // 获取赛制信息
            RaceCompetitionRules rules = raceCompetitionRulesService.selectRaceCompetitionRulesById(competitionDetail.getRulesId());
            
            return success(rules);
        } catch (Exception e) {
            return error("获取赛制信息失败：" + e.getMessage());
        }
    }

    /**
     * 新增竞赛详情
     */
    @PreAuthorize("@ss.hasPermi('race:competitionDetail:add')")
    @Log(title = "竞赛详情", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RaceCompetitionDetail raceCompetitionDetail)
    {
        return toAjax(raceCompetitionDetailService.insertRaceCompetitionDetail(raceCompetitionDetail));
    }

    /**
     * 修改竞赛详情
     */
    @PreAuthorize("@ss.hasPermi('race:competitionDetail:edit')")
    @Log(title = "竞赛详情", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RaceCompetitionDetail raceCompetitionDetail)
    {
        return toAjax(raceCompetitionDetailService.updateRaceCompetitionDetail(raceCompetitionDetail));
    }

    /**
     * 删除竞赛详情
     */
    @PreAuthorize("@ss.hasPermi('race:competitionDetail:remove')")
    @Log(title = "竞赛详情", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(raceCompetitionDetailService.deleteRaceCompetitionDetailByIds(ids));
    }
}
